using B.S.BasicData.Domain.TSH;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Write.Api.Application.Command.TSH;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BasicData.Write.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class OnlineBoxesController : ControllerBase
    {
        private readonly IMediator _mediator;

        public OnlineBoxesController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 查询在线箱子列表
        /// </summary>
        /// <param name="command">查询参数</param>
        /// <returns>在线箱子列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<OnlineBoxes>>> GetOnlineBoxes([FromQuery] QueryOnlineBoxesCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 添加在线箱子
        /// </summary>
        /// <param name="command">添加参数</param>
        /// <returns>新增的在线箱子ID</returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateOnlineBox([FromBody] CreateOnlineBoxCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 更新在线箱子
        /// </summary>
        /// <param name="id">在线箱子ID</param>
        /// <param name="command">更新参数</param>
        /// <returns>更新的在线箱子ID</returns>
        [HttpPut("{id}")]
        public async Task<ApiResult<int>> UpdateOnlineBox(int id, [FromBody] UpdateOnlineBoxCommand command)
        {
            if (id != command.Id)
            {
                return new ApiResult<int> { Code = ApiEnums.Error, Msg = "ID不匹配" };
            }
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 删除在线箱子
        /// </summary>
        /// <param name="id">在线箱子ID</param>
        /// <returns>删除的在线箱子ID</returns>
        [HttpDelete("{id}")]
        public async Task<ApiResult<int>> DeleteOnlineBox(int id)
        {
            var command = new DeleteOnlineBoxCommand { Id = id };
            return await _mediator.Send(command);
        }
    }
} 